package com.sgb.kjwl.viewmodel.self.logistics;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.CarriageInfoPublishEntity;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.model.entity.self.GoodsSelectEntity;
import com.sgb.kjwl.model.entity.self.ReleaseGoodsEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.MapGoodsBiddingEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.logistics.OverbookingPublishActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
        *   承运议价
        *
        *   说明：承运商机  ----》 议价 (列表界面)
        *
        * */
public class OverbookingPublishModel extends BaseViewModel {

   OverbookingPublishActivity activity;
    private SelfRepertory repertory;

    public OverbookingPublishModel(OverbookingPublishActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }


    public ObservableField<CarriageInfoPublishEntity> carriageInfoPublishEntity = new ObservableField<>(new CarriageInfoPublishEntity());

    public ObservableField<ReleaseGoodsEntity> releaseGoodsEntityObservableField = new ObservableField<>(new ReleaseGoodsEntity());

    //这个是弹框中的引用  GoodsSourceWindow 中的相关引用 7/20
    public ObservableField<MapGoodsBiddingEntity> sourceDetailEntity = new ObservableField<>(new MapGoodsBiddingEntity());



    //发货地  +  目的地  联合使用
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    activity.showAddressPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }




    //货物类型
    public void goodsType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().goodsType().enqueue(new Callback<BaseEntity<List<GoodsSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsSelectEntity>>> call, Response<BaseEntity<List<GoodsSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showGoodsType(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }

            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }


    //是否自卸
    public void isDump() {
        CarriageSelectEntity one = new CarriageSelectEntity();
        CarriageSelectEntity two = new CarriageSelectEntity();
        List<CarriageSelectEntity> list = new ArrayList<>();
        one.setName("是");
        one.setNo("1");
        two.setName("否");
        two.setNo("0");
        list.add(one);
        list.add(two);
        activity.showIsDumpPop(list);
    }



    //我的车型
    public void carType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().carType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarModelPop(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //我的车长
    public void carLenType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().carLenType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarLengthPop(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }




    //装载方式
    public void getHandleType(){
        APIManager.getInstance().getDriverAPI().getHandleType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showhandlingType(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //付款方式
    public void getPayType(){
        APIManager.getInstance().getDriverAPI().getPayType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;


                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showPayType(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }



    //发票要求
    public void invoiceType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().invoiceType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showInvoiceType(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }



    //运输方式
    public void getTransType(){
        APIManager.getInstance().getDriverAPI().getTransType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showTransType(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //发布货源   提交数据 上传
    public void releaseGoodsSubmit() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().releaseGoodsSubmit(releaseGoodsEntityObservableField.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.finish();
                    MToast.showToast(activity, "推送成功");
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }



    //选择货源
    public void getGoodsSourceModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        Map map = new HashMap();
        map.put("page","1");
        map.put("limit","10");

        APIManager.getInstance().getSelfOwnerAPI().myGoodsSourceList(map).enqueue(new Callback<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> call, Response<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> response) {

                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.showGoodsSource(response.body().getData().getList());
                } else {
                    assert response.body() != null;
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<MapGoodsBiddingEntity>>>> call, Throwable t) {

                DialogHelper.dismissProgressDialog();

            }
        });
    }


    //议价   提交数据 上传
    public void bargining(){
        DialogHelper.showProgressDialog(activity, "提交中", 0, false);
        APIManager.getInstance().getTransAPI().counterOffer(sourceDetailEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (!response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, response.body().getMsg());
                    MToast.showToast(activity, "推送成功");
                    return;
                }

                activity.finish();

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //下单     提交数据 上传
    public void ordering(){
        DialogHelper.showProgressDialog(activity, "提交中", 0, false);
        APIManager.getInstance().getTransAPI().placeOrder(sourceDetailEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (!response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, response.body().getMsg());
                    MToast.showToast(activity, "推送成功");
                    return;
                }

                activity.finish();
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }




}
